//
//  LayerUtils.m
//  iosOpenP
//
//  Created by liupm on 14-8-7.
//
//

#import "TKIMLayerUtils.h"

static TKIMLayerUtils *layer;
@interface TKIMLayerUtils()
//等待层
@property(nonatomic,strong)  TKIMProgressHUD *showLoading;
//等待层是否已经存在
@property(nonatomic,assign) BOOL isShowing;
//主窗口
@property(nonatomic,strong) UIWindow *window;
//提示容器
@property(nonatomic,strong) UIView *contentView;

@end

@implementation TKIMLayerUtils
{
}

+(TKIMLayerUtils *)instance
{
    if (!layer)
    {
        layer = [[TKIMLayerUtils alloc] init];
    }
    return layer;
}



-(id)init
{
    self = [super init];
    if (self)
    {
        self.isLockScreen = YES;
        _isShowing  = NO;
        [self.window addSubview:self.showLoading];
    }
    return self;
}


-(id)initWithContent:(UIView *)contentView
{
    self = [super init];
    if (self) {
        _contentView = contentView;
        self.isLockScreen = YES;
        _isShowing  = NO;
        [_contentView addSubview:self.showLoading];
    }
    return self;
}


/**
 *  隐藏显示等待层
 *  @param isShow 显示或者隐藏
 *  @param text   文字
 */
-(void)showLoading:(BOOL)isShow withLabel:(NSString *)text
{
    //如果不是自定义的窗口
    if (!_contentView)
    {
        //如果当前提示层的窗口不是当前的主窗口
        if (self.showLoading.superview != self.window)
        {
            [self.showLoading removeFromSuperview];
            [self.window addSubview:self.showLoading];
        }
    }

    self.showLoading.userInteractionEnabled = _isLockScreen;
    if (!text)
    {
        self.showLoading.labelText = @"请等待...";
    }
    else
    {
        self.showLoading.labelText = text;
    }
    if (isShow)
    {
        if (!_isShowing)
        {
            _isShowing = YES;
            [self.showLoading show:YES];
            [self.showLoading hide:YES afterDelay:2.0];
        }
    }
    else
    {
        if (_isShowing)
        {
            _isShowing = NO;
            [self.showLoading hide:NO];
        }
    }
}


/**
 *  默认文字等待层
 */
-(void)showLoading:(BOOL)isShow
{
    [self showLoading:isShow withLabel:nil];
}


/**
 *  @Author lpm, 16-05-05 17:05:06
 *
 *  @brief  短提示
 *
 *  @param tip
 */
-(void)showTip:(NSString *)tip
{
    [self showTip:tip position:TKIMLayerTipPositionBottom];
}


/**
 *  @Author lpm, 16-05-05 17:05:55
 *
 *  @brief
 *
 *  @param tip
 *  @param delay
 */
-(void)showTip:(NSString *)tip  hideAfterDelay:(NSTimeInterval)delay
{
    [self showTip:tip position:TKIMLayerTipPositionBottom hideAfterDelay:delay];
}


/**
 *  @Author lpm, 16-05-05 17:05:43
 *
 *  @brief  短提示
 *
 *  @param tip
 *  @param position
 */
-(void)showTip:(NSString *)tip position:(TKIMLayerTipPosition)position
{
    [self showTip:tip position:position hideAfterDelay:2.0];
}



/**
 *  @Author lpm, 16-05-05 17:05:47
 *
 *  @brief
 *
 *  @param tip
 *  @param position
 *  @param delay
 */
-(void)showTip:(NSString *)tip position:(TKIMLayerTipPosition)position  hideAfterDelay:(NSTimeInterval)delay
{
    NSString *postionStr = @"bottom";
    
    if (position == TKIMLayerTipPositionCenter)
    {
        postionStr = @"center";
    }
    else if(position == TKIMLayerTipPositionTop)
    {
        postionStr = @"top";
    }
    
    if(_contentView)
    {
        [_contentView makeToast:tip duration:delay position:postionStr];
    }
    else
    {
        [self.window makeToast:tip duration:delay position:postionStr];
    }
}


#pragma mark 属性懒加载
-(TKIMProgressHUD *)showLoading
{
    if (!_showLoading)
    {
        _showLoading =  [[TKIMProgressHUD alloc] init];
    }
    return _showLoading;
}


-(UIWindow *)window
{
    return [UIApplication sharedApplication].keyWindow;
}

@end
